package com.SinaMicrobloggingAPI.Core.Process;

import java.util.ArrayList;
import java.util.List;

import com.SinaMicrobloggingAPI.Core.Request.AccessTokenRequest;
import com.SinaMicrobloggingAPI.Core.Request.AuthorizeRequest;
import com.SinaMicrobloggingAPI.Core.Request.PublicTimeLineRequest;
import com.SinaMicrobloggingAPI.Core.Request.RequestTokenRequest;
import com.SinaMicrobloggingAPI.Core.Request.StatusesUpdateRequest;
import com.SinaMicrobloggingAPI.Core.Request.VerifyCredentialsRequest;
import com.SinaMicrobloggingAPI.Core.Response.AccessTokenResponse;
import com.SinaMicrobloggingAPI.Core.Response.AuthorizeResponse;
import com.SinaMicrobloggingAPI.Core.Response.PublicTimeLineResponse;
import com.SinaMicrobloggingAPI.Core.Response.RequestTokenResponse;
import com.SinaMicrobloggingAPI.Core.Response.StatusesUpdateResponse;
import com.SinaMicrobloggingAPI.Core.Response.VerifyCredentialsResponse;
import com.SinaMicrobloggingAPI.Model.StatusModel;
import com.SinaMicrobloggingAPI.Model.UserInfoModel;

public class SinaMicrobloggingAPIProcess {
	public static UserInfoModel logging(String userName, String password) {
		// 1.first to get request token
		String token = "";
		String tokenSecret = "";
		RequestTokenResponse requestTokenresponse = new RequestTokenResponse();
		RequestTokenRequest requestTokenRequest = new RequestTokenRequest(
				requestTokenresponse);
		requestTokenresponse = (RequestTokenResponse) requestTokenRequest
				.getResponse();
		token = requestTokenresponse.getOauthToken();
		tokenSecret = requestTokenresponse.getOauthTokenSecret();
		// 2.to get Authorize
		AuthorizeResponse authorizeResponse = new AuthorizeResponse();
		AuthorizeRequest authorizeRequest = new AuthorizeRequest(
				authorizeResponse);
		authorizeRequest.setUserId(userName);
		authorizeRequest.setPassword(password);
		authorizeRequest.setOauthToken(token);
		authorizeRequest.setOauthTokenSecret(tokenSecret);
		authorizeResponse = (AuthorizeResponse) authorizeRequest.getResponse();
		// 3. to get AccessToken
		AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
		AccessTokenRequest accessTokenRequest = new AccessTokenRequest(
				accessTokenResponse);
		accessTokenRequest.setOauthVerifier(authorizeResponse
				.getOauthVerifier());
		accessTokenRequest.setOauthToken(token);
		accessTokenRequest.setOauthTokenSecret(tokenSecret);

		accessTokenResponse = (AccessTokenResponse) accessTokenRequest
				.getResponse();
		token = accessTokenResponse.getOauthToken();
		tokenSecret = accessTokenResponse.getOauthTokenSecret();
		// confirm personal information
		VerifyCredentialsResponse verifyCredentialsResponse = new VerifyCredentialsResponse();
		VerifyCredentialsRequest verifyCredentialsRequest = new VerifyCredentialsRequest(
				verifyCredentialsResponse);
		verifyCredentialsRequest.setOauthToken(token);
		verifyCredentialsRequest.setOauthTokenSecret(tokenSecret);
		verifyCredentialsResponse = (VerifyCredentialsResponse) verifyCredentialsRequest
				.getResponse();
		UserInfoModel user = verifyCredentialsResponse.getUserInfo();
		user.setAccessToken(token);
		user.setAccessTokenSecret(tokenSecret);
		user.setVerifier(authorizeResponse.getOauthVerifier());

		return user;
	}

	public static List<StatusModel> getPublicTimeLine(String accessToken,
			String accessTokenSecret, int count) {
		PublicTimeLineResponse publicTimeResponse = new PublicTimeLineResponse();
		PublicTimeLineRequest publicTimeRequest = new PublicTimeLineRequest(
				publicTimeResponse, count);
		publicTimeRequest.setOauthToken(accessToken);
		publicTimeRequest.setOauthTokenSecret(accessTokenSecret);
		publicTimeResponse = (PublicTimeLineResponse) publicTimeRequest
				.getResponse();

		return publicTimeResponse.getStatus();
	}

	public static boolean postANewStatus(String accessToken,
			String accessTokenSecret, String verifier, String userId,
			String password, String content) {

		StatusesUpdateResponse statusesUpdateResponse = new StatusesUpdateResponse();
		StatusesUpdateRequest statusesUpdateRequest = new StatusesUpdateRequest(
				statusesUpdateResponse, userId, password, content);
		statusesUpdateRequest.setOauthToken(accessToken);
		statusesUpdateRequest.setOauthTokenSecret(accessTokenSecret);
		statusesUpdateRequest.setVerifier(verifier);
		statusesUpdateResponse = (StatusesUpdateResponse) statusesUpdateRequest
				.getResponse();

		return false;
	}
}
